<?php
// +-------------------------------------------------------------------------+
// | PHP Version 5                                                           |
// +-------------------------------------------------------------------------+
// | ImaEngine - web development framework                                   |
// | Copyright (c) 2006-2007 Sardar Yumatov <ja.doma@gmail.com>              |
// +-------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it |
// | under the terms of the GNU Lesser General Public License as published   |
// | by the Free Software Foundation; either version 2.1 of the License, or  |
// | (at your option) any later version.                                     |
// |                                                                         |
// | This library is distributed in the hope that it will be useful, but     |
// | WITHOUT ANY WARRANTY; without even the implied warranty of              |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    |
// | See the GNU Lesser General Public License for more details.             |
// |                                                                         |
// | You should have received a copy of the GNU Lesser General Public        |
// | License along with this library; if not, write to the Free Software     |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
// +-------------------------------------------------------------------------+

/**
* Global environment variables
*
* Script builds $environment variable which contains many usefull things like user IP.
* New things may be added later on demand.
*
* Currently this script builds the $environment variable with following keys:
*
*   - $environment['host'] -- the host name provided by the server
*   - $environment['user-ip'] -- the client's IP address
*
*
* Configuration used:
*  - [global]str:host - host where we are installed (fallback)
*
* @author Sardar Yumatov <ja.doma@gmail.com>
* @copyright Copyright &copy; 2006-2007 Sardar Yumatov
* @package boot
*
* @export $environment
*/

if(!defined('PROCESS')) {
	header('HTTP/1.1 404 Not Found');
	die('NotFound');
}

require_once('lib/Configuration.php');

//$environment variable is available
define('PROCESS_ENVIRONMENT', true);


//global info variable
$environment=array();

//current host, used for absolute URL's
if(isset($_SERVER["SERVER_NAME"])) $environment['host'] = $_SERVER["SERVER_NAME"];
else $environment['host'] = Configuration::getOption('[global]str:host');

//======- User IP -===========
$remote_addr = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']: '0.0.0.0';
if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
	$client_ip=$_SERVER['HTTP_CLIENT_IP'];
	$ip_expl = explode('.',$client_ip);
	$referer = explode('.',$remote_addr);
	if($referer[0] != $ip_expl[0]) $remote_addr = implode('.', array_reverse($ip_expl));
	else $remote_addr = $client_ip;
	unset($client_ip, $ip_expl, $referer);
} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
	$x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
	if(strstr($x_forwarded_for,',')) {
		$ip_expl = explode(',',$x_forwarded_for);
		$remote_addr = end($ip_expl); //last of IP list
	} else $remote_addr = $x_forwarded_for;
	unset($x_forwarded_for, $ip_expl);
}
$environment['user-ip']=$remote_addr;
unset($remote_addr);

?>